#include "chip.h"
#include "mainwindow.h"
#include "view.h"
#include <QHBoxLayout>
#include <qevent.h>
#include <QMimeData>
#include <QTextCodec>
#include <QFileInfo>
#include <QDir>

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent), scene(new QGraphicsScene(this))
{
    setAcceptDrops(true);
    //populateScene();
    View *view = new View("display svg tiny 1.2");
    connect(view, &View::signalDragList, [this](QStringList list){
        m_listFile = list;
        populateScene();
    });
    view->view()->setScene(scene);
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(view);
    setLayout(layout);
    setWindowTitle(tr("display SVG"));
}

void MainWindow::populateScene()
{
    scene->clear();
    int cnt = 0;
    int row = m_listFile.size()/10;
    row = m_listFile.size() % 10 > 0 ? row +1 : row;
    int column = 10;
    // Populate scene
    for (int i = 0; i < row ; i ++) {
        if(i == row -1) {
            column = (m_listFile.size() % 10 > 0) ? m_listFile.size() % 10 : 10;
        }
        for (int j = 0; j < column; j ++) {
            QGraphicsItem *item = new Chip(m_listFile.value(cnt), 0, 0);
            item->setPos(QPointF(i * 100, j * 70));
            scene->addItem(item);
            cnt++;
        }
    }
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    //判断是否包含url
    if(event->mimeData()->hasUrls()){
        //接收动作
        event->acceptProposedAction();
    }else{
        //放弃事件
        event->ignore();
    }
}

void getAllFiles(const QString &dirPath, QStringList &fileList)
{
    QDir dir(dirPath);
    QFileInfoList list = dir.entryInfoList(QStringList(), QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs, QDir::SortFlag::Time);
    for(const QFileInfo &fileInfo : list){
        if(fileInfo.isDir()){
            QImage image(fileInfo.absoluteFilePath());
            if (!image.isNull()) {
                fileList.append(fileInfo.absoluteFilePath()); //即是这里 fileInfo.isDir() 为真 也要使用 fileInfo.absoluteFilePath()而不是 fileInfo.absolutePath()
            }
            getAllFiles(fileInfo.absoluteFilePath(), fileList);
        }else{
            QImage image(fileInfo.absoluteFilePath());
            if (!image.isNull()) {
                fileList.append(fileInfo.absoluteFilePath());
            }
        }
    }
}


void MainWindow::dropEvent(QDropEvent * event)
{
    m_listFile.clear();
    const QMimeData * mimeData = event->mimeData();
    //判断是否包含url
    if(mimeData->hasUrls()){
        QList<QUrl> urlList = mimeData->urls();
        for(int i = 0; i<urlList.size(); i++) {
            QString filename = urlList[i].toLocalFile();
            QFileInfo fileInfo(filename);
            if(fileInfo.isFile()) {
                QImage image(filename);
                if (!image.isNull()) {
                    m_listFile << filename;
                }
            }
            if(fileInfo.isDir()) {
                    getAllFiles(filename, m_listFile);
            }
            populateScene();
        }
    }else{
        event->ignore();
    }
}
